VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "CLS_IE"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Dim iPercentageComplete As Integer

Public WithEvents oIE As InternetExplorer
Attribute oIE.VB_VarHelpID = -1

' Update remote database with browser ----------------------------------------------------------------------------------------------------

Public Sub updateWithBrowser(sURL As String, sPostData As String)

    On Error Resume Next
    
    logMessage _
        iDEBUG, _
        "CLS_IE", _
        "updateWithBrowser method call", _
        "URL = " & sURL & _
            " | POST data = " & sPostData
        
    If oIE Is Nothing Then

        On Error Resume Next

        Set oIE = New InternetExplorer
        
        If Err.Number <> 0 Then
        
            logMessage _
                iERROR, _
                "CLS_IE", _
                "Could not create InternetExplorer object", _
                , _
                Err.Number, _
                Err.Description
                
            MsgBox "Could not update records" & _
                vbCrLf & _
                "Reason : could not create Internet Explorer object"
                
            DLGupdating.terminate
                
            Exit Sub
            
        Else
        
            logMessage _
                iDEBUG, _
                "CLS_IE", _
                "Created InternetExplorer object"

        End If
    
    End If
    
    ' Set display properties for IE --------------------------------------------------------------------------------
    
    oIE.Visible = False
    
    oIE.AddressBar = False
    oIE.Toolbar = False
    oIE.MenuBar = False
    oIE.StatusBar = True
    
    oIE.StatusText = "Updating"
    
    oIE.Width = 300
    oIE.Height = 300
    
    Dim bPostData() As Byte
    
    bPostData = getPostData(sPostData)
    
    oIE.Navigate2 sURL, , , bPostData, "Content-Type:application/x-www-form-urlencoded"
    
    logMessage _
        iINFO, _
        "CLS_IE", _
        "Navigate2 method of InternetExplorer object called", _
        "URL = " & sURL & _
            " | POST data = " & sPostData & _
            " | From file ? " & MODweb.bUpdatedFromFile
            
    DLGupdating.init _
        "Updating Remote Database", _
        "Please wait while the remote database is updated", _
        vbModal

End Sub

' Is called when update is comepleted, regardless of success ----------------------------------------------------------------------------

Private Sub updateComplete(oWindow As Object)

    On Error Resume Next

    Dim sSuccess As String
    
    Dim oIEdoc
    
    On Error Resume Next
    
    Set oIEdoc = oWindow.Document
    
    If Err.Number <> 0 Then
    
        logMessage _
            iWARNING, _
            "CLS_IE", _
            "Could not get IE.document object after documentComplete event", _
            Err.Number, _
            Err.Description
            
    Else
    
        logMessage _
            iDEBUG, _
            "CLS_IE", _
            "Got IE.document object after documentComplete event"
    
    End If

    On Error Resume Next

    sSuccess = LCase(oIEdoc.successStatus)
    
    If Err.Number <> 0 Then
    
        logMessage _
            iWARNING, _
            "CLS_IE", _
            "Could not get successStatus property of IE.document object", _
            "InnerHTML of body = " & oIE.Document.body.innerHTML, _
            Err.Number, _
            Err.Description
        
    Else
    
        logMessage _
            iINFO, _
            "CLS_IE", _
            "successStatus property of IE.document object returned as : " & sSuccess
    
    End If
    
    If sSuccess = "success" Then
             
        MsgBox "All records have been updated successfully"
        
        If MODweb.bUpdatedFromFile = True Then
        
            Dim fso As Scripting.FileSystemObject
             
            Dim sBackupPath As String
             
            Set fso = New Scripting.FileSystemObject
             
            sBackupPath = MODweb.sRemoteUpdateBackupDirPath & "records_" & MODapplicationMain.getUniqueTimeStamp
            
            If Not fso.FolderExists(MODweb.sRemoteUpdateBackupDirPath) Then fso.CreateFolder (MODweb.sRemoteUpdateBackupDirPath)
             
            fso.MoveFile MODweb.sRemoteUpdateRecordsFilePath, sBackupPath
            
            If Err.Number <> 0 Then
            
                fso.CopyFile MODweb.sRemoteUpdateRecordsFilePath, sBackupPath
            
            End If
             
            MODweb.bUpdatedFromFile = False
            
        Else
        
            MODweb.resetStatementArray
        
        End If
        
        FRMmain.bRemoteSaved = True
    
    Else
    
        MsgBox "The records could not be updated." & vbCrLf & _
            "It is recommended that you save the records for updating later"
            
        FRMmain.bRemoteSaved = False
    
    End If
    
    oIE.Quit
    
    Set oIE = Nothing

End Sub

' Fires when the document is completely loaded ----------------------------------------------------------------------------
' This event will not fire in IE 5.5 or previous versions, when Visible property is false --------------------------

Private Sub oIE_DocumentComplete(ByVal pDisp As Object, URL As Variant)

    On Error Resume Next
    
    DLGupdating.terminate
    
    updateComplete pDisp

End Sub

' this event is always fired after a navigation starts. Any animation or "busy" indication that the container needs to display should be connected to this event. - MSDN

'Private Sub oIE_DownloadComplete()

'    On Error Resume Next

'    DLGupdating.terminate
    
'   updateComplete

'End Sub

Private Sub oIE_NavigateError(ByVal pDisp As Object, URL As Variant, Frame As Variant, StatusCode As Variant, Cancel As Boolean)

    On Error Resume Next

    DLGupdating.terminate

End Sub

' Quit event for browser -------------------------------------------------------------------------------------------------------------------

Private Sub oIE_OnQuit()

    On Error Resume Next
    
    DLGupdating.terminate

    Set oIE = Nothing
    
End Sub

Private Sub oIE_ProgressChange(ByVal Progress As Long, ByVal ProgressMax As Long)

    On Error Resume Next

    If ProgressMax < 1 Then Exit Sub

    iPercentageComplete = Progress * 100 / ProgressMax
    
    DLGupdating.ProgressBar.Value = iPercentageComplete
    
    Debug.Print Progress & " / " & ProgressMax & " / " & iPercentageComplete

End Sub
